package com.atguigu.guli.service.ucenter.controller.api;

import com.atguigu.guli.common.util.FormUtils;
import com.atguigu.guli.service.base.dto.MemberDto;
import com.atguigu.guli.service.base.helper.JwtHelper;
import com.atguigu.guli.service.base.helper.JwtInfo;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.ucenter.entity.form.LoginForm;
import com.atguigu.guli.service.ucenter.entity.form.RegisterForm;
import com.atguigu.guli.service.ucenter.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "会员管理")
@RestController

@RequestMapping("/api/ucenter/member")
@Slf4j
public class ApiMemberController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("会员注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterForm registerForm){

        String mobile = registerForm.getMobile();
        String nickname = registerForm.getNickname();
        String password = registerForm.getPassword();
        String code = registerForm.getCode();

        //校验参数
        if (StringUtils.isEmpty(mobile) || !FormUtils.isMobile(mobile)
                || StringUtils.isEmpty(password) || StringUtils.isEmpty(nickname)
                || StringUtils.isEmpty(code)){
            return R.setResult(ResultCodeEnum.PARAM_ERROR);
        }

        //校验验证码
        String key = "checkCode::" + mobile;
        String checkCode = (String) redisTemplate.opsForValue().get(key);
        if (!code.equals(checkCode)){
            return R.setResult(ResultCodeEnum.CODE_ERROR);
        }

        memberService.register(registerForm);
        return R.ok().message("注册成功");

    }

    @ApiOperation("会员登录")
    @PostMapping("login")
    public R login(@RequestBody LoginForm loginForm){
        String mobile = loginForm.getMobile();
        String password = loginForm.getPassword();
        //校验参数不为空
        if (StringUtils.isEmpty(mobile)
                || !FormUtils.isMobile(mobile)
                || StringUtils.isEmpty(password)){
            return R.setResult(ResultCodeEnum.PARAM_ERROR);
        }

        String token = memberService.login(loginForm);
        return R.ok().data("token",token).message("登录成功");
    }

    @ApiOperation("根据token获取登录信息")
    @GetMapping("auth/getUserInfo")
    public R getLoginInfo(HttpServletRequest request){
        JwtInfo jwtInfo = JwtHelper.getJwtInfo(request);
        return R.ok().data("userInfo",jwtInfo);
    }

    @ApiOperation("根据会员id查询会员信息")
    @GetMapping("getMemberDto/{memberId}")
    public R getMemberDtoById(@PathVariable String memberId){
        MemberDto memberDto = memberService.getMemberDtoById(memberId);
        return R.ok().data("memberDto",memberDto);
    }

    @ApiOperation("根据日期统计注册人数")
    @GetMapping("countRegisterNum/{day}")
    public R countRegisterNum(@PathVariable String day){
        Integer num = memberService.countRegisterNum(day);
        return R.ok().data("registerNum",num);
    }

}

